package com.fly.estore.controller;

import com.fly.estore.domain.ModuleInfo;
import com.fly.estore.framework.common.AjaxResult;
import com.fly.estore.service.ModuleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Fly on 2016/4/13.
 */
@Controller
@RequestMapping("/system/module")
public class ModuleController {

    @Autowired
    ModuleService moduleService;

    @RequestMapping("/list")
    public String moduleList(
            ModelMap model
    ){
        List<ModuleInfo> moduleList = moduleService.getModuleList();
        model.addAttribute("moduleList", moduleList);
        return "/system/module/list";
    }

    @RequestMapping("/detail")
    public String moduleDetail(
            @RequestParam(value = "moduleId", required = false, defaultValue = "")String moduleId,
            ModelMap model
    ){
        ModuleInfo moduleInfo = new ModuleInfo();
        if (StringUtils.isNotBlank(moduleId)){
            moduleInfo = moduleService.getModuleById(moduleId);
        }
        model.addAttribute("module", moduleInfo);
        model.addAttribute("parentModuleList", moduleService.getModuleList());
        return "/system/module/detail";
    }

    @RequestMapping("/save")
    @ResponseBody
    public AjaxResult saveModuleInfo(
            @RequestParam(value = "moduleId", required = false, defaultValue = "")String moduleId,
            @RequestParam(value = "moduleName", required = false, defaultValue = "")String moduleName,
            @RequestParam(value = "moduleArea", required = false ,defaultValue = "")String moduleArea,
            @RequestParam(value = "moduleHref", required = false, defaultValue = "")String moduleHref,
            @RequestParam(value = "parentModuleId", required = false, defaultValue = "")String parentModuleId,
            @RequestParam(value = "moduleIcon", required = false, defaultValue = "")String moduleIcon,
            @RequestParam(value = "orderNum", required = false, defaultValue = "0")int orderNum,
            @RequestParam(value = "showMenu", required = false, defaultValue = "")String showMenu,
            @RequestParam(value = "level", required = false, defaultValue = "0")int level
    ){
        ModuleInfo moduleInfo = new ModuleInfo();
        moduleInfo.setModuleId(moduleId);
        moduleInfo.setModuleName(moduleName);
        moduleInfo.setModuleArea(moduleArea);
        moduleInfo.setModuleHref(moduleHref);
        ModuleInfo parentModule = new ModuleInfo();
        parentModule.setModuleId(parentModuleId);
        moduleInfo.setParentModule(parentModule);
        moduleInfo.setModuleIcon(moduleIcon);
        moduleInfo.setOrderNum(orderNum);
        moduleInfo.setShowMenu(showMenu);
        moduleInfo.setLevel(level);
        int update = moduleService.saveModuleInfo(moduleInfo);

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return ajaxResult;
    }

    @RequestMapping("/getModuleList")
    @ResponseBody
    public AjaxResult getModuleList(){
        List<ModuleInfo>  moduleList = moduleService.getModuleList();
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setStatus(AjaxResult.STATUS_SUCCESS);
        ajaxResult.setData(moduleList);
        return ajaxResult;
    }
}
